package web.PetStore.web.servlet;

import web.PetStore.domain.Product;
import web.PetStore.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SearchProductsFormServlet", urlPatterns = {"/SearchProductsForm"})
public class SearchProductsFormServlet extends HttpServlet {
    private static final String SEARCH_PRODUCTS_FORM = "WEB-INF/jsp/catalog/SearchProducts.jsp";
    private ProductService productService = new ProductService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String info = req.getParameter("searchInfo");
        List<Product> productList = productService.searchProducts(info);
        if(info.isEmpty()){
            productList = null;
        }
        req.setAttribute("productList", productList);
        req.getRequestDispatcher(SEARCH_PRODUCTS_FORM).forward(req,resp);
    }
}
